package com.simple.datax.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.simple.datax.bo.IDataxDictionaryBO;
import com.simple.datax.bo.IDataxEnterpriseChannelBO;
import com.simple.datax.bo.IDataxReceiveChannelBO;
import com.simple.datax.bo.IDataxSendChannelBO;
import com.simple.datax.bo.IVwDataxEnterpriseBO;
import com.simple.datax.vo.DataxEnterpriseChannelIdVO;
import com.simple.datax.vo.DataxEnterpriseChannelVO;
import com.simple.datax.vo.DataxReceiveChannelVO;
import com.simple.datax.vo.DataxSendChannelVO;
import com.simple.datax.vo.EnterpriseChannelDetailVO;
import com.simple.datax.vo.VwDataxEnterpriseVO;
import com.simple.util.common.Constants;
import com.simple.util.common.ErrorUtil;
import com.simple.util.exception.SimpleApplicationException;
import com.simple.util.object.JFValidationBean;
import com.simple.util.object.PageInfoBean;

@Controller 
public class PassChannelController extends AbstractDataxBaseController{
	private final static String JSP_PATH = "_PassChannel";
	
	/** The LOG. */
	private static Logger LOG = Logger.getLogger(PassChannelController.class);
	
	@Autowired
	private IVwDataxEnterpriseBO vwDataxEnterpriseBO;
	
	@Autowired
	private IDataxEnterpriseChannelBO dataxEnterpriseChannelBO;
	
	@Autowired
	private IDataxDictionaryBO dataxDictionaryBO;
	
	@Autowired
	private IDataxReceiveChannelBO dataxReceiveChannelBO;
	
	@Autowired
	private IDataxSendChannelBO dataxSendChannelBO;

	@RequestMapping("/passChannel")  
    public String init(@RequestParam(value="pageNoEnterprise",required=false,defaultValue="1") Integer pageNoEnterprise,
    		   @RequestParam(value="pageNoChannel",required=false,defaultValue="1") Integer pageNoChannel,
			   @RequestParam(value="ecCodeSelect",required=false) String ecCodeSelect,
			   @RequestParam(value="ecNameSelect",required=false) String ecNameSelect,
			   @RequestParam(value="fcEntcode",required=false) String fcEntcode,
			   final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into PassChannelController.init");
		Map<String, String> optionFiFlag = Constants.STATE_MAP;
	    request.setAttribute("optionFiFlag", optionFiFlag);
	    
	    Map<String, String> optionCommunTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_TRANSFERSWAY);
        request.setAttribute("optionCommunTypeMap", optionCommunTypeMap);
        
        Map<String, String> optionMsgTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGETYPE);
        request.setAttribute("optionMsgTypeMap", optionMsgTypeMap);
        
        Map<String, String> optionMsgFormatMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGEFORMAT);
        request.setAttribute("optionMsgFormatMap", optionMsgFormatMap);
        
        Map<String, String> optionMsgCharsetMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGECHARACTER);
        request.setAttribute("optionMsgCharsetMap", optionMsgCharsetMap);
        
		Map<String, String> optionServeTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_SERVECODE);
        request.setAttribute("optionServeTypeMap", optionServeTypeMap);
        //查询列表
		final PageInfoBean pageInfoBeanEnterprise = new PageInfoBean(pageNoEnterprise);
        final List<VwDataxEnterpriseVO> lsEnterprise = vwDataxEnterpriseBO.findListByPage(ecCodeSelect, ecNameSelect, pageInfoBeanEnterprise);
        request.setAttribute("vwDataxEnterpriseVOList", lsEnterprise);
        
		final PageInfoBean pageInfoBeanChannel = new PageInfoBean(pageNoChannel);
        List<EnterpriseChannelDetailVO> lsChannel = new ArrayList<EnterpriseChannelDetailVO>();
        if (!StringUtils.isBlank(fcEntcode)){
        	lsChannel = dataxEnterpriseChannelBO.findAllDetailByPage(fcEntcode, null, pageInfoBeanChannel);
            request.setAttribute("dataxEnterpriseChannelVOListSize", lsChannel.size());
            request.setAttribute("dataxEnterpriseChannelVOList", lsChannel);
        }
        request.setAttribute("dataxEnterpriseChannelVOList", lsChannel);
        
		final String pageInfoEnterprise = super.generatePageInfo(request,response,pageInfoBeanEnterprise, "goPageEnterprise");
	    request.setAttribute("pageInfoEnterprise", pageInfoEnterprise);  
        request.setAttribute("pageNoEnterprise", pageNoEnterprise);
        
		final String pageInfoChannel = super.generatePageInfo(request,response,pageInfoBeanChannel, "goPageChannel");
	    request.setAttribute("pageInfoChannel", pageInfoChannel);  
        request.setAttribute("pageNoChannel", pageNoChannel);
        
        request.setAttribute("ecCodeSelect", ecCodeSelect);
        request.setAttribute("ecNameSelect", ecNameSelect);
        request.setAttribute("fcEntcode", fcEntcode);
        return getJspPath("pass_channel");
    }
	
	@RequestMapping("/passChannelAdd")  
    public String add(@RequestParam(value="pageNoEnterprise",required=false,defaultValue="1") Integer pageNoEnterprise,
				@RequestParam(value="pageNoChannel",required=false,defaultValue="1") Integer pageNoChannel,
				@RequestParam(value="ecCodeSelect",required=false) String ecCodeSelect,
    			@RequestParam(value="fcEntcode",required=false) String fcEntcode,
    			@RequestParam(value="fiSysReceiveId",required=false) String fiSysReceiveId,
    			@RequestParam(value="fiSysSendId",required=false) String fiSysSendId,
			    @RequestParam(value="ecNameSelect",required=false) String ecNameSelect,
			    final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into PassChannelController.passChannelAddInit");
		VwDataxEnterpriseVO vwDataxEnterpriseVO = vwDataxEnterpriseBO.findById(fcEntcode);
		
		//下拉框
		
		Map<String,String> fcSysSendIdMap =dataxSendChannelBO.findSysSendIdMap();
		request.setAttribute("fcSysSendIdMap", fcSysSendIdMap);
		
		Map<String,String> fcSysReceiveIdMap =dataxReceiveChannelBO.findSysReceiveIdMap();
		request.setAttribute("fcSysReceiveIdMap", fcSysReceiveIdMap);
		
		Map<String, String> optionCommunTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_TRANSFERSWAY);
        request.setAttribute("optionCommunTypeMap", optionCommunTypeMap);
        
        Map<String, String> optionMsgTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGETYPE);
        request.setAttribute("optionMsgTypeMap", optionMsgTypeMap);
        
        Map<String, String> optionMsgFormatMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGEFORMAT);
        request.setAttribute("optionMsgFormatMap", optionMsgFormatMap);
        
        Map<String, String> optionMsgCharsetMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGECHARACTER);
        request.setAttribute("optionMsgCharsetMap", optionMsgCharsetMap);
        
		Map<String, String> optionServeTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_SERVECODE);
        request.setAttribute("optionServeTypeMap", optionServeTypeMap);
        
        Map<String, String> optionFiFlagMap = Constants.STATE_MAP;
        request.setAttribute("optionFiFlagMap", optionFiFlagMap);
        
        //页面赋值
        request.setAttribute("pageNoEnterprise", pageNoEnterprise);
        request.setAttribute("pageNoChannel", pageNoChannel);
        request.setAttribute("ecCodeSelect", ecCodeSelect);
        request.setAttribute("ecNameSelect", ecNameSelect);
        request.setAttribute("fiSysReceiveId", fiSysReceiveId);
        request.setAttribute("fiSysSendId", fiSysSendId);
        request.setAttribute("fcEntcode", fcEntcode);

        request.setAttribute("vwDataxEnterpriseVO", vwDataxEnterpriseVO);
        return getJspPath("pass_channel_add");
    }
	

	
	@RequestMapping("/passChannelAddSubmit")  
    public String addSubmit(@RequestParam(value="pageNoEnterprise",required=false,defaultValue="1") Integer pageNoEnterprise,
			    @RequestParam(value="pageNoChannel",required=false,defaultValue="1") Integer pageNoChannel,
    			@RequestParam(value="ecCodeSelect",required=false) String ecCodeSelect,
			    @RequestParam(value="ecNameSelect",required=false) String ecNameSelect,
    			@RequestParam(value="ecCode",required=false) String ecCode,
    			@RequestParam(value="fcEntcode",required=false) String fcEntcode,
			    @ModelAttribute("dataxEnterpriseChannelVO") DataxEnterpriseChannelVO dataxEnterpriseChannelVO,
			    @ModelAttribute("dataxEnterpriseChannelIdVO") DataxEnterpriseChannelIdVO dataxEnterpriseChannelIdVO,
			    final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into PassChannelController.passChannelAddSubmit");
		
		if(null != dataxEnterpriseChannelBO.findById(dataxEnterpriseChannelIdVO)){
			final List<JFValidationBean> errorList = getErrorList();
			errorList.add(ErrorUtil.generateJFValidationFromAppException(new SimpleApplicationException("message.businessflow.DataRepeat")));
			return add(1, 1, ecCodeSelect, fcEntcode, dataxEnterpriseChannelVO.getFiSysReceiveId().toString(), dataxEnterpriseChannelVO.getFiSysSendId().toString(), ecNameSelect, request, response);
		}
		
		dataxEnterpriseChannelVO.setId(dataxEnterpriseChannelIdVO);
		dataxEnterpriseChannelBO.add(dataxEnterpriseChannelVO);
		
        return init(pageNoEnterprise, pageNoChannel, ecCodeSelect, ecNameSelect, fcEntcode, request, response);
    }
	
	@RequestMapping("/passChannelEdit")  
	public String edit(@RequestParam(value="pageNoEnterprise",required=false,defaultValue="1") Integer pageNoEnterprise,
			@RequestParam(value="pageNoChannel",required=false,defaultValue="1") Integer pageNoChannel,
			@RequestParam(value="ecCodeSelect",required=false) String ecCodeSelect,
		    @RequestParam(value="ecNameSelect",required=false) String ecNameSelect,
			@RequestParam(value="fcEntcode",required=false) String fcEntcode,
			@RequestParam(value = "fcServicecode", required = false) String fcServicecode,
			@RequestParam(value = "fiSysReceiveId", required = false) String fiSysReceiveId,
			@RequestParam(value = "fiSysSendId", required = false) String fiSysSendId,
			final HttpServletRequest request, final HttpServletResponse response) {
		// 下拉框
		
		Map<String, String> optionServeTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_SERVECODE);
		request.setAttribute("optionServeTypeMap", optionServeTypeMap);

		Map<String, String> optionFiFlagMap = Constants.STATE_MAP;
		request.setAttribute("optionFiFlagMap", optionFiFlagMap);

		Map<String, String> optionCommunTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_TRANSFERSWAY);
		request.setAttribute("optionCommunTypeMap", optionCommunTypeMap);

		Map<String, String> optionMsgTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGETYPE);
		request.setAttribute("optionMsgTypeMap", optionMsgTypeMap);

		Map<String, String> optionMsgFormatMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGEFORMAT);
		request.setAttribute("optionMsgFormatMap", optionMsgFormatMap);

		Map<String, String> optionMsgCharsetMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGECHARACTER);
		request.setAttribute("optionMsgCharsetMap", optionMsgCharsetMap);

		DataxReceiveChannelVO dataxReceiveChannelVO = null;
		DataxSendChannelVO dataxSendChannelVO = null;
		DataxEnterpriseChannelVO dataxEnterpriseChannelVO = null;
		VwDataxEnterpriseVO vwDataxEnterpriseVO = vwDataxEnterpriseBO.findById(fcEntcode);
		
		DataxEnterpriseChannelIdVO dataxEnterpriseChannelIdVO = new DataxEnterpriseChannelIdVO();
		dataxEnterpriseChannelIdVO.setFcEntcode(fcEntcode);
		dataxEnterpriseChannelIdVO.setFcServicecode(fcServicecode);
		dataxEnterpriseChannelVO = dataxEnterpriseChannelBO.findById(dataxEnterpriseChannelIdVO);
		
		dataxReceiveChannelVO = dataxReceiveChannelBO.findById(dataxEnterpriseChannelVO.getFiSysReceiveId());
		dataxSendChannelVO = dataxSendChannelBO.findById(dataxEnterpriseChannelVO.getFiSysSendId());
		request.setAttribute("fiSysSendId", dataxSendChannelVO.getFiId());
		request.setAttribute("fiSysReceiveId", dataxReceiveChannelVO.getFiId());
		request.setAttribute("communParamRcv", dataxReceiveChannelVO.getFcCommunParam());
        request.setAttribute("communTypeRcv", optionCommunTypeMap.get(dataxReceiveChannelVO.getFcCommunType()));
        request.setAttribute("msgFormateIdRcv", optionMsgFormatMap.get(dataxReceiveChannelVO.getFcMsgFormateId()));
        request.setAttribute("msgCharsetRcv", optionMsgCharsetMap.get(dataxReceiveChannelVO.getFcMsgCharset()));
        request.setAttribute("msgTypeRcv", optionMsgTypeMap.get(dataxReceiveChannelVO.getFcMsgType()));
        request.setAttribute("communParamSnd", dataxSendChannelVO.getFcCommunParam());
        request.setAttribute("communTypeSnd", optionCommunTypeMap.get(dataxSendChannelVO.getFcCommunType()));
        request.setAttribute("msgFormateIdSnd", optionMsgFormatMap.get(dataxSendChannelVO.getFcMsgFormateId()));
        request.setAttribute("msgCharsetSnd", optionMsgCharsetMap.get(dataxSendChannelVO.getFcMsgCharset()));
        request.setAttribute("msgTypeSnd", optionMsgTypeMap.get(dataxSendChannelVO.getFcMsgType()));
		
		 //页面赋值
        request.setAttribute("pageNoEnterprise", pageNoEnterprise);
        request.setAttribute("pageNoChannel", pageNoChannel);
        request.setAttribute("ecCodeSelect", ecCodeSelect);
        request.setAttribute("ecNameSelect", ecNameSelect);
        request.setAttribute("fcEntcode", fcEntcode);
        request.setAttribute("dataxEnterpriseChannelVO", dataxEnterpriseChannelVO);
        request.setAttribute("vwDataxEnterpriseVO", vwDataxEnterpriseVO);
		return getJspPath("pass_channel_edit");
	}
	
	@RequestMapping("/passChannelEditSubmit")  
	public String editSubmit(@RequestParam(value="pageNoEnterprise",required=false,defaultValue="1") Integer pageNoEnterprise,
			@RequestParam(value="pageNoChannel",required=false ,defaultValue="1") Integer pageNoChannel,
			@RequestParam(value="ecCodeSelect",required=false) String ecCodeSelect,
		    @RequestParam(value="ecNameSelect",required=false) String ecNameSelect,
		    @RequestParam(value="fcEntcode",required=false) String fcEntcode,
		    @ModelAttribute("dataxEnterpriseChannelVO") DataxEnterpriseChannelVO dataxEnterpriseChannelVO,
		    @ModelAttribute("dataxEnterpriseChannelIdVO") DataxEnterpriseChannelIdVO dataxEnterpriseChannelIdVO,
		    final HttpServletRequest request, final HttpServletResponse response)  {
		dataxEnterpriseChannelVO.setId(dataxEnterpriseChannelIdVO);
		dataxEnterpriseChannelBO.edit(dataxEnterpriseChannelVO);
		
		 //页面赋值
        request.setAttribute("ecCodeSelect", ecCodeSelect);
        request.setAttribute("ecNameSelect", ecNameSelect);
        return init(pageNoEnterprise, pageNoChannel, ecCodeSelect, ecNameSelect, fcEntcode,request, response);
	}
	
	@RequestMapping("/passChanneldel")  
	public String del(@RequestParam(value="pageNoEnterprise",required=false,defaultValue="1") Integer pageNoEnterprise,
			@RequestParam(value="pageNoChannel",required=false,defaultValue="1") Integer pageNoChannel,
			@RequestParam(value="ecCodeSelect",required=false) String ecCodeSelect,
		    @RequestParam(value="ecNameSelect",required=false) String ecNameSelect,
			@RequestParam(value="fcEntcode",required=false) String fcEntcode,
			@RequestParam(value="fcServicecode",required=false) String fcServicecode,
		    @ModelAttribute("dataxEnterpriseChannelIdVO") DataxEnterpriseChannelIdVO dataxEnterpriseChannelIdVO,
		    final HttpServletRequest request, final HttpServletResponse response)  {
		dataxEnterpriseChannelBO.del(dataxEnterpriseChannelIdVO);
		
		 //页面赋值
        request.setAttribute("ecCodeSelect", ecCodeSelect);
        request.setAttribute("ecNameSelect", ecNameSelect);
        return init(1, 1, ecCodeSelect, ecNameSelect, fcEntcode,request, response);
	}
	
	@Override
    protected String getBaseJspPath() {
        return JSP_PATH;
    }
}